﻿namespace QuotaCheck.QuotaCheck
{
    using System;
    using System.Web.UI;
    using Microsoft.SharePoint;

    public partial class QuotaCheckUserControl : UserControl
    {
        /// <summary>
        /// Saves the percent usage from given MaxQuota.
        /// </summary>
        private int percentQuotaUse = 0;

        /// <summary>
        /// Saves the given max quota.
        /// </summary>
        private long maxQuota = 0;

        /// <summary>
        /// Saves the given warning quota.
        /// </summary>
        private long warningQuota = 0;

        /// <summary>
        /// Saves the current usage in bytes
        /// </summary>
        private long currentStorageUse = 0;

        /// <summary>
        /// Override the site content render
        /// </summary>
        /// <param name="writer">output stream for writing markup content</param>
        protected override void Render(HtmlTextWriter writer)
        {
            string warningbgcolor = "red";
            string warningfontcolor = "black";
            string fontcolor = "black";
            string backcolor = "#AAF0FF";

            if (!string.IsNullOrEmpty(QuotaCheck.warningBackColor))
            {
                warningbgcolor = QuotaCheck.warningBackColor;
            }

            if (!string.IsNullOrEmpty(QuotaCheck.warningFontColor))
            {
                warningfontcolor = QuotaCheck.warningFontColor;
            }

            if (!string.IsNullOrEmpty(QuotaCheck.backColor))
            {
                backcolor = QuotaCheck.backColor;
            }

            if (!string.IsNullOrEmpty(QuotaCheck.fontColor))
            {
                fontcolor = QuotaCheck.fontColor;
            }

            try
            {
                this.CalculateQuotaUse();

                if (this.maxQuota > 0)
                {
                    if (this.currentStorageUse < this.warningQuota)
                    {
                        writer.Write("<div class='progress_bar'><strong style='color:" + fontcolor + "';>" + this.percentQuotaUse + "%" + "</strong><span style='background-color:" + backcolor + ";width:" + this.percentQuotaUse + "%;'>&nbsp;</span></div>");
                    }
                    else
                    {
                        writer.Write("<div class='progress_bar_warn'><strong style='color:" + warningfontcolor + "';>" + this.percentQuotaUse + "%" + "</strong><span style='background-color:" + warningbgcolor + ";width:" + this.percentQuotaUse + "%;'>&nbsp;</span></div>");
                    }

                    writer.Write("<strong>" + "Current use: " + (this.currentStorageUse / (1024 * 1024)) + " MB<strong><br>");
                    writer.Write("<strong>" + "warning quota: " + (this.warningQuota / (1024 * 1024)) + " MB<strong><br>");
                    writer.Write("<strong>" + "maximum quota: " + (this.maxQuota / (1024 * 1024)) + " MB<strong><br>");
                }
                else
                {
                    writer.Write("<strong>There is no quota available.<strong><br>");
                }
            }
            catch (Exception)
            {
                writer.Write("<strong>error while getting quota information<strong><br>");
            }
        }

        /// <summary>
        /// Calculating the usage and fill the warn,max quota size
        /// </summary>
        private void CalculateQuotaUse()
        {
            SPSite siteCollection = SPContext.Current.Site;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite elevatedSiteCollection = new SPSite(siteCollection.ID))
                {
                    if (elevatedSiteCollection.Quota.StorageMaximumLevel > 0)
                    {
                        this.maxQuota = elevatedSiteCollection.Quota.StorageMaximumLevel;
                        this.warningQuota = elevatedSiteCollection.Quota.StorageWarningLevel;
                        this.currentStorageUse = elevatedSiteCollection.Usage.Storage;
                        this.percentQuotaUse = Convert.ToInt32(this.currentStorageUse * 100 / this.maxQuota);
                    }
                }
            });
        }
    }
}
